package com.indusfo.spc.client.controller;

import com.indusfo.spc.annotation.UserLoginToken;
import com.indusfo.spc.client.pojo.ClientAno;
import com.indusfo.spc.client.pojo.ClientAnoDetail;
import com.indusfo.spc.client.pojo.ClientMat;
import com.indusfo.spc.client.service.ClientAnoDetailService;
import com.indusfo.spc.client.service.ClientBadphenoService;
import com.indusfo.spc.client.service.ClientMatService;
import com.indusfo.spc.client.service.ClientRepairService;
import com.indusfo.spc.common.pojo.BasePojo;
import com.indusfo.spc.vo.JSONObject;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhengd
 * @create 2019-12-04
 */
@RestController
@RequestMapping("/client/repair")
public class ClientRepairController {

	@Autowired
	private ClientRepairService clientRepairService;
	@Autowired
	private ClientAnoDetailService clientAnoDetailService;
	@Autowired
	private ClientBadphenoService clientBadphenoService;
	@Autowired
	private ClientMatService clientMatService;

	/**
	 * 扫码进站及查询返修基础信息
	 * 
	 * @param ClientAno
	 * @return
	 */
	@PostMapping("/queryRepair")
	@UserLoginToken
	public JSONObject queryRepair(@RequestBody ClientAno clientAno) {
		try {
			return JSONObject.oK(clientRepairService.queryRepair(clientAno));
		} catch (Exception e) {
			return JSONObject.build(JSONObject.ERROR, e.getMessage());
		}
	}

	/**
	 * 根据批号ID查询全部工序
	 * 
	 * @param batchId
	 * @return
	 */
	@PostMapping("/selectAllProByBatchId")
	@UserLoginToken
	public JSONObject selectAllProByBatchId(@RequestBody Map<String, Integer> map) {
		try {
			return JSONObject.oK(clientRepairService.selectAllProByBatchId(map.get("batchId")));
		} catch (Exception e) {
			return JSONObject.build(JSONObject.ERROR, "系统错误，请联系管理员！");
		}
	}

	/**
	 * 查询该单子下所有不良现象
	 * 
	 * @param batchId
	 * @return
	 */
	@PostMapping("/queryClientAnoDetail")
	@UserLoginToken
	public JSONObject queryClientAnoDetail(@RequestBody Map<String, Integer> map) {
		try {
			return JSONObject.oK(clientAnoDetailService.queryClientAnoDetail(map.get("batchId")));
		} catch (Exception e) {
			return JSONObject.build(JSONObject.ERROR, "系统错误，请联系管理员！");
		}
	}

	/**
	 * 根据不良现象查询不良原因
	 * 
	 * @param badphenoId
	 * @return
	 */
	@PostMapping("/queryBadcauseByBadpheno")
	@UserLoginToken
	public JSONObject queryBadcauseByBadpheno(@RequestBody Map<String, Integer> map) {
		try {
			return JSONObject.oK(clientBadphenoService.queryBadcauseByBadpheno(map.get("badphenoId")));
		} catch (Exception e) {
			return JSONObject.build(JSONObject.ERROR, "系统错误，请联系管理员！");
		}
	}

	/**
	 * 根据不良现象查询不良对策
	 * 
	 * @param badphenoId
	 * @return
	 */
	@PostMapping("/queryBadreplyByBadpheno")
	@UserLoginToken
	public JSONObject queryBadreplyByBadpheno(@RequestBody Map<String, Integer> map) {
		try {
			return JSONObject.oK(clientBadphenoService.queryBadreplyByBadpheno(map.get("badphenoId")));
		} catch (Exception e) {
			return JSONObject.build(JSONObject.ERROR, "系统错误，请联系管理员！");
		}
	}

	/**
	 * 保存不良原因和对策
	 * 
	 * @param clientAnoDetail
	 * @return
	 */
	@PostMapping("/saveClientAnoDetail")
	@UserLoginToken
	public JSONObject saveClientAnoDetail(@RequestBody ClientAnoDetail clientAnoDetail) {
		try {
			return JSONObject.oK(clientAnoDetailService.saveClientAnoDetail(clientAnoDetail));
		} catch (Exception e) {
			return JSONObject.build(JSONObject.ERROR, e.getMessage());
		}
	}

	/**
	 * 物料替换，根据批号查询关键物料
	 * 
	 * @param batchId
	 * @return
	 */
	@PostMapping("/queryClientMatByBatchId")
	@UserLoginToken
	public JSONObject queryClientMatByBatchId(@RequestBody Map<String, Integer> map) {
		try {
			return JSONObject.oK(clientMatService.queryClientMatByBatchId(map.get("batchId")));
		} catch (Exception e) {
			e.printStackTrace();
			return JSONObject.build(JSONObject.ERROR, "系统错误，请联系管理员！");
		}
	}

	/**
	 * 扫新物料保存
	 * 
	 * @param ClientMat
	 * @return
	 */
	@PostMapping("/updateClientMat")
	@UserLoginToken
	public JSONObject updateClientMat(@RequestBody ClientMat clientMat) {
		try {
			return JSONObject.oK(clientMatService.updateClientMat(clientMat));
		} catch (Exception e) {
			e.printStackTrace();
			return JSONObject.build(JSONObject.ERROR, e.getMessage());
		}
	}

	/**
	 * 维修完工
	 * 
	 * @param
	 * @return
	 */
	@PostMapping("/repairFinish")
	@UserLoginToken
	public JSONObject repairFinish(@RequestBody ClientAno clientAno) {
		try {
			return JSONObject.oK(clientRepairService.repairFinish(clientAno));
		} catch (Exception e) {
			return JSONObject.build(JSONObject.ERROR, e.getMessage());
		}
	}

	/**
	 * 查询未进站批次
	 * 
	 * @param
	 * @return
	 */
	@PostMapping("/queryNoEntryBatch")
	@UserLoginToken
	public JSONObject queryNoEntryBatch(@RequestBody BasePojo basePojo) {
		try {
			return JSONObject.oK(clientRepairService.queryNoEntryBatch(basePojo),
					clientRepairService.countQueryNoEntryBatch());
		} catch (Exception e) {
			return JSONObject.build(JSONObject.ERROR, "系统错误，请联系管理员！");
		}
	}

	/**
	 * 查询已进站批次
	 * 
	 * @param
	 * @return
	 */
	@PostMapping("/queryArrivalBatch")
	@UserLoginToken
	public JSONObject queryArrivalBatch(@RequestBody Map<String, Integer> map) {
		try {
			return JSONObject.oK(clientRepairService.queryArrivalBatch(map.get("stationId")));
		} catch (Exception e) {
			return JSONObject.build(JSONObject.ERROR, "系统错误，请联系管理员！");
		}

	}
}
